using Koala.Pro.Common;
using System.Globalization;
using System.Windows.Data;

namespace Koala.Pro.Helpers;

public class Long2FileSizeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string result = "未知大小";
        if (value == null)
        {
            return result;
        }
        long num = value.ToLong(0L);
        if (num > 0)
        {
            if (num < 1024)
            {
                return $"{num} B";
            }
            if (num < 1048576)
            {
                return $"{(double)num / 1024.0:0.00} KB";
            }
            if (num < 1073741824)
            {
                return $"{(double)num / 1048576.0:0.00} MB";
            }
            if (num < 1099511627776L)
            {
                return $"{(double)num / 1073741824.0:0.00} GB";
            }
            if (num < 1125899906842624L)
            {
                return $"{(double)num / 1099511627776.0:0.00} TB";
            }
            if (num < 1152921504606847000L)
            {
                return $"{(double)num / 1125899906842624.0:0.00} PB";
            }
            return "过大";
        }
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
